/*
 * OpenFilesDialog.java
 *
 * Created on 27. mars 2004, 23:19
 */

package ps2vfs.gui;
import javax.swing.table.*;

/**
 *
 * @author  stigpo
 */
public class OpenFilesDialog extends javax.swing.JDialog {
    
  private ps2vfs.vfs.Ps2Vfs vfs;
  
  /** Creates new form OpenFilesDialog */
  public OpenFilesDialog(java.awt.Frame parent, boolean modal, 
			 ps2vfs.vfs.Ps2Vfs vfs) {
    super(parent, modal);
    this.vfs = vfs;
    initComponents();

    jOpenFilesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);

    refreshModel(false);
  }

  public void refreshModel(boolean keepColumnWidths) {
    int[] columnWidths = new int[5];
    TableColumn column = null;
    if(keepColumnWidths) {
      for (int i = 0; i < 5; i++) {
	column = jOpenFilesTable.getColumnModel().getColumn(i);
	if(column != null) {
	  columnWidths[i] = column.getPreferredWidth();
	} else {
	  columnWidths[i] = 100;
	}
      }
    }

    initModel();
    int totalColumnWidth = 0;
    if(keepColumnWidths) {
      for (int i = 0; i < 5; i++) {
	column = jOpenFilesTable.getColumnModel().getColumn(i);
	column.setPreferredWidth(columnWidths[i]);
	totalColumnWidth += columnWidths[i];
      }
    } else {
      for (int i = 0; i < 5; i++) {
	column = jOpenFilesTable.getColumnModel().getColumn(i);
	if (i == 0) {
	  column.setPreferredWidth(30);
	  column.setMinWidth(30);
	  totalColumnWidth += 30;
	} else if(i == 1) {
	  column.setPreferredWidth(50);
	  column.setMinWidth(50);
	  totalColumnWidth += 50;
	} else if(i == 4) {
	  column.setPreferredWidth(400);
	  column.setMinWidth(400);
	  totalColumnWidth += 400;
	} else {
	  column.setPreferredWidth(150);
	  column.setMinWidth(150);
	  totalColumnWidth += 150;
	}
      }
    }

    int width = jScrollPane1.getWidth();
    if(width > totalColumnWidth) {
      column = jOpenFilesTable.getColumnModel().getColumn(4);
      int lastColumnWidth = column.getWidth();
      column.setPreferredWidth(lastColumnWidth + (width - totalColumnWidth));
    }
  }

  private void initModel() {
    ps2vfs.vfs.VfsOpenFileInfo[] ofi = vfs.getOpenFilesInfo();
    Object [][] strTbl = null;
    if(ofi != null && ofi.length > 0) {
      strTbl = new Object[ofi.length][5];
      for(int r = 0; r < ofi.length; r++) {
	strTbl[r][0] = "" + ofi[r].getFD();
	strTbl[r][1] = ofi[r].getIPStr();
	strTbl[r][2] = ofi[r].getVPath();
	strTbl[r][3] = ofi[r].getOPath();
	String info = ofi[r].getInfo();
	strTbl[r][4] = info != null ? info : "";
      }
    }
    jOpenFilesTable.setModel(new javax.swing.table.DefaultTableModel(
				     strTbl,
				     new String [] {
				       "FileID", "Client", "Virtual path", "Actual path", 
				       "Info" }
				     ));
  }
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        jOpenFilesTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setTitle("Open Files");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(453, 100));
        jScrollPane1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                viewportResized(evt);
            }
        });

        jOpenFilesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jOpenFilesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jOpenFilesTable.setRowSelectionAllowed(false);
        jScrollPane1.setViewportView(jOpenFilesTable);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jButton1.setText("Refresh");
        jButton1.setToolTipText("Refresh the list of open files");
        jButton1.setMaximumSize(new java.awt.Dimension(50, 26));
        jButton1.setMinimumSize(new java.awt.Dimension(50, 26));
        jButton1.setPreferredSize(new java.awt.Dimension(50, 26));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionHandler(evt);
            }
        });

        getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);

        pack();
    }//GEN-END:initComponents

    private void viewportResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_viewportResized
        // Add your handling code here:
      // System.out.println("ViewportResized: " + evt);
      java.awt.Component comp = evt.getComponent();
      resizeTable(comp);
    }//GEN-LAST:event_viewportResized

  private void resizeTable(java.awt.Component comp) {
    int width = comp.getWidth();
    int tableWidth = jOpenFilesTable.getWidth();
    //System.out.println("Width: " + width + " tableWidth: " + tableWidth);
    if(width > tableWidth) {
      TableColumn column = jOpenFilesTable.getColumnModel().getColumn(4);
      int lastColumnWidth = column.getWidth();
      if(false) {
	System.out.println("Adjusting last column with: " + 
			   (lastColumnWidth + (width - tableWidth)));
      }
      column.setWidth((lastColumnWidth + (width - tableWidth)));
      column.setPreferredWidth((lastColumnWidth + (width - tableWidth)));

      java.awt.Dimension d = jOpenFilesTable.getSize();
      d.width = d.width + (width - tableWidth);
      jOpenFilesTable.setSize(d);
    }
  }

    private void actionHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionHandler
        // Add your handling code here:
      //System.out.println("Action event: " + evt);
      if(evt.getActionCommand().equals("Refresh")) {
	refreshModel(true);
      }
    }//GEN-LAST:event_actionHandler
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTable jOpenFilesTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}

